<?php

class HomeController extends BaseController {

	public function showHome() {
            $relatos = $this->getEnditadesPublicas('Relato', 7, 'imagerel_relato');
            $objetos = $this->getEnditadesPublicas('Objeto', 4, 'imageobj_objeto');
            $apartment = Apartment::active()->orderBy('fecha', 'desc')->take(1)->get();
   //         $apartment = Apartment::getFirst();
            return View::make('home', [
                        'relatos' => $relatos,
                        'objetos' => $objetos,
                        'apartment' => $apartment,
                    ]
            );
        }
        // para relatos y objetos
        public function getEnditadesPublicas($entidad, $cantidad, $tablaPivot){
            
            $imagePosition = $tablaPivot . '.position';
            
            return $entidad::active()
                    ->sinAnonimos()
                    ->with([
                        'tags',
                        'imagesOrdered' => function($query) use ($imagePosition) {$query->where($imagePosition, 0);}
                        ])
                    ->orderBy('position')
                    ->take($cantidad)
                    ->get();
        }
        
        public function getFirstApartment(){
            $apartment = Apartment::active()->orderBy('fecha', 'desc')->take(1)->first();
//                    DB::table('relatos')->join('relato_tag_rel', 'relatos.id', '=', 'relato_tag_rel.relato_id')
//                                ->orderBy('position') 
//                                ->where('relato_tag_rel.tag_rel_id','=',1)
//                                ->first();
            return $apartment;
        }
        
}
